<?php
function formatLength($text)
{
    $secs = (int)$text;
    $mins = 0;
    if ($secs>59) {
        $mins = $secs/60;
        $secs = $secs%60;
        if ($mins>59) {
            $hours = $mins/60;
            $mins = $mins%60;
        }
    }
    if ($hours)
        return sprintf("%d:%02d:%02d", $hours, $mins, $secs);
    else
        return sprintf("%d:%02d", $mins, $secs);
}

function format($context, $md, $show_title)
{
    return "";
}

function formatAudio($context, $md, $show_title)
{
    $res = '';
    if ($artist = $md["audio:artist"]) $res .= $context->translate("Artist").":&nbsp;$artist. ";
    if ($title = $md["audio:title"]) $res .= $context->translate("Title").":&nbsp;$title. ";
    if ($album = $md["audio:album"]) {
        $res .= $context->translate("Album").":&nbsp;$album";
        if (($year = $md["audio:year"]) && is_numeric($year) && $year>1901 && $year<2100)
            $res .= "&nbsp;($year). ";
        else
            $res .= ". ";
    }
    if ($genre = $md["audio:genre"]) $res .= $context->translate("Genre").":&nbsp;$genre. ";
    if ($len = $md["audio:seconds"]) $res .= $context->translate("Length").":&nbsp;".formatLength($len).". ";
    if ($bitrate = $md["audio:bitrate"]) $res .= $context->translate("Bitrate").":&nbsp;$bitrate&nbsp;kbit/s. ";
    return $res;
}

function formatDocument($context, $md, $show_title)
{
    $res = '';
    if ($show_title && $title = $md["document:title"]) $res .= "$title<br/>";
    if ($pages = $md["document:pages"]) $res .= $context->translate("Nº of pages").":&nbsp;$pages. ";
    return $res;
}

function formatImage($context, $md, $show_title)
{
    $res = '';
    if ($show_title && $title = $md["image:title"]) $res .= "$title<br/>";
    if (($width = $md["image:width"]) && ($height = $md["image:height"]))
        $res .= $context->translate("Size").":&nbsp;${width}x$height. ";
    if ($colors = $md["image:colors"]) $res .= $context->translate("Colors").":&nbsp;$colors. ";
    return $res;
}

function formatVideo($context, $md, $show_title)
{
    $res = '';
    if ($show_title && $title = $md["video:title"]) $res .= "$title<br/>";
    if (($len = $md["video:minutes"]*60) || ($len = $md["video:length"]))
        $res .= $context->translate("Length").":&nbsp;".formatLength($len).". ";
    if (($width = $md["video:width"]) && ($height = $md["video:height"]))
        $res .= $context->translate("Size").":&nbsp;${width}x$height. ";
    return $res;
}

function formatSoftware($context, $md, $show_title)
{
    $res = '';
    if ($show_title && $title = $md["application:title"]) $res .= "$title<br/>";
    return $res;
}

function formatArchive($context, $md, $show_title)
{
    $res = '';
    if ($files = $md["archive:files"])
        $res .= $context->translate("Files").":&nbsp;$files. ";
    return $res;
}
?>

<div id="container_search">
    <h1>
        <a href="/" ><img border="0" src="<?=STATIC_PATH ;?>/images/foofind_search.gif" alt="File search engine - foofind.com" title="File search engine - foofind.com" /></a>
    </h1>

<?=$this->form; ?>
    <div class="contentype">
        <a class="<?=(preg_match("/^$/",  $this->qs['type']))?"actual":""?>" href="<?= ''.$this->qs(array(), array('type'=>1, 'page'=>1)) ?>" ><?=$this->translate('All');?></a>
        <a class="<?=(preg_match("/^Audio$/", $this->qs['type']))?"actual":""?>" href="<?= $this->qs(array('type'=>'Audio'), array('page'=>1)) ?>" ><?=$this->translate('Audio');?></a>
        <a class="<?=(preg_match("/^Video$/", $this->qs['type']))?"actual":""?>" href="<?= $this->qs(array('type'=>'Video'), array('page'=>1)) ?>" ><?=$this->translate('Video');?></a>
        <a class="<?=(preg_match("/^Image$/", $this->qs['type']))?"actual":""?>" href="<?= $this->qs(array('type'=>'Image'), array('page'=>1)) ?>" ><?=$this->translate('Image');?></a>
        <a class="<?=(preg_match("/^Document$/", $this->qs['type']))?"actual":""?>" href="<?= $this->qs(array('type'=>'Document'), array('page'=>1)) ?>" ><?=$this->translate('Document');?></a>
        <a class="<?=(preg_match("/^Software$/", $this->qs['type']))?"actual":""?>" href="<?= $this->qs(array('type'=>'Software'), array('page'=>1)) ?>" ><?=$this->translate('Software');?></a>
        <a class="<?=(preg_match("/^Archive$/", $this->qs['type']))?"actual":""?>" href="<?= $this->qs(array('type'=>'Archive'), array('page'=>1)) ?>" ><?=$this->translate('Archive');?></a>
    </div>

<?php if ($this->info['total']>0): ?>
    <div class="search_info"><?=$this->info['start']?> - <?=$this->info['end'].' ';?><?=$this->translate("of about").' '?>
        <?=number_format($this->info['total'], 0) ?> <?=$this->translate("results for")?>
            <?php foreach (explode(' ', $this->info['q']) as $word) { echo "&nbsp;<a class='actual' href='".$this->qs(array('q'=>$word), array('page'=>1))."'>$word</a>"; }?><?php if (APPLICATION_ENV!='production'):?><div class="debuginfo">total:<?=number_format($this->info['time'],3)?> - <?=$this->info['time_desc']?></div>
        <style>    .debuginfo {   font-size: 9px;    /* display:none !important; */   }    </style><?php endif; ?></div>
<?php endif;?>

    <ul id="tabnav">
        <li><a class="<?=(preg_match("/^wftge$/",  $this->qs['src']))?"tab_actual":""?>" href="<?=$this->qs(array('src'=>'wftge'), array('page'=>1))?>"><?=$this->translate('All');?></a></li>
        <li><a class="<?=(preg_match("/^wf$/",  $this->qs['src']))?"tab_actual":""?>" href="<?=$this->qs(array('src'=>'wf'), array('page'=>1)) ?>" ><?=$this->translate('Direct downloads');?></a></li>
        <li><a class="<?=(preg_match("/^t$/",  $this->qs['src']))?"tab_actual":""?>" href="<?=$this->qs(array('src'=>'t'), array('page'=>1))?>"><?=$this->translate('Torrents');?></a></li>
        <li><a class="<?=(preg_match("/^s$/",  $this->qs['src']))?"tab_actual":""?>" href="<?=$this->qs(array('src'=>'s'), array('page'=>1)) ?>" ><?=$this->translate('Streaming');?></a></li>
        <li><a class="<?=(preg_match("/^g$/",  $this->qs['src']))?"tab_actual":""?>" href="<?=$this->qs(array('src'=>'g'), array('page'=>1)) ?>" ><?=$this->translate('Gnutella');?></a></li>
        <li><a class="<?=(preg_match("/^e$/",  $this->qs['src']))?"tab_actual":""?>" href="<?=$this->qs(array('src'=>'e'), array('page'=>1)) ?>" ><?=$this->translate('Ed2k');?></a></li>
    </ul>
   
        <?
if (count ( $this->mensajes ) > 0) {
	echo '<div class="success">';
	foreach ( $this->mensajes as $msg ) {
		echo $msg . '<br />';
	}
	echo '</div>';
}
?>

    <div id="socontainer"><a id="show_options" href="#" active="<?=$this->qs['opt']?>"><?$this->translate("advanced seach")?></a></div>
    <div id="advsearch">
    <div id="common">
        <ul class="size">
            <li><a <?=(preg_match("/^$/", $this->qs['size']))?"class='actual'":"href='".$this->qs(array(), array('size'=>1, 'page'=>1))."'"?>><?=$this->translate('All sizes')?></a></li>
            <li><a <?=(preg_match("/^1$/", $this->qs['size']))?"class='actual'":"href='".$this->qs(array('size'=>'1', 'opt'=>1), array('page'=>1))."'"?>><?=$this->translate('Smaller than');?> 1MB</a></li>
            <li><a <?=(preg_match("/^2$/", $this->qs['size']))?"class='actual'":"href='".$this->qs(array('size'=>'2', 'opt'=>1), array('page'=>1))."'"?>><?=$this->translate('Smaller than');?> 10MB</a></li>
            <li><a <?=(preg_match("/^3$/", $this->qs['size']))?"class='actual'":"href='".$this->qs(array('size'=>'3', 'opt'=>1), array('page'=>1))."'"?>><?=$this->translate('Smaller than');?> 100MB</a></li>
            <li><a <?=(preg_match("/^4$/", $this->qs['size']))?"class='actual'":"href='".$this->qs(array('size'=>'4', 'opt'=>1), array('page'=>1))."'"?>><?=$this->translate('Larger than');?> 100MB</a></li>
        </ul>
    </div>
        <?php if (preg_match("/^Audio$/", $this->qs['type'])): ?>
        <ul class="quality">
            <li><a <?=(preg_match("/^$/", $this->qs['brate']))?"class='actual'":"href='".$this->qs(array(), array('brate'=>1, 'page'=>1))."'"?>><?=$this->translate('All qualities');?></a></li>
            <li><a <?=(preg_match("/^1$/", $this->qs['brate']))?"class='actual'":"href='".$this->qs(array('brate'=>'1', 'opt'=>1), array('page'=>1))."'"?>>128 kbps <?=$this->translate('or better');?></a></li>
            <li><a <?=(preg_match("/^2$/", $this->qs['brate']))?"class='actual'":"href='".$this->qs(array('brate'=>'2', 'opt'=>1), array('page'=>1))."'"?>>192 kbps <?=$this->translate('or better');?></a></li>
            <li><a <?=(preg_match("/^3$/", $this->qs['brate']))?"class='actual'":"href='".$this->qs(array('brate'=>'3', 'opt'=>1), array('page'=>1))."'"?>>256 kbps <?=$this->translate('or better');?></a></li>
            <li><a <?=(preg_match("/^4$/", $this->qs['brate']))?"class='actual'":"href='".$this->qs(array('brate'=>'4', 'opt'=>1), array('page'=>1))."'"?>>320 kbps <?=$this->translate('or better');?></a></li>
        </ul>
        <?php endif; ?>
        <?php if (preg_match("/^Audio|Video$/", $this->qs['type'])): ?>
        <ul class="year">
            <li><a <?=(preg_match("/^$/", $this->qs['year']))?"class='actual'":"href='".$this->qs(array(), array('year'=>1, 'page'=>1))."'"?>><?=$this->translate('All years');?></a></li>
            <li><a <?=(preg_match("/^1$/", $this->qs['year']))?"class='actual'":"href='".$this->qs(array('year'=>'1', 'opt'=>1), array('page'=>1))."'"?>><?=$this->translate('Before');?> 1960</a></li>
            <li><a <?=(preg_match("/^2$/", $this->qs['year']))?"class='actual'":"href='".$this->qs(array('year'=>'2', 'opt'=>1), array('page'=>1))."'"?>>60's</a></li>
            <li><a <?=(preg_match("/^3$/", $this->qs['year']))?"class='actual'":"href='".$this->qs(array('year'=>'3', 'opt'=>1), array('page'=>1))."'"?>>70's</a></li>
            <li><a <?=(preg_match("/^4$/", $this->qs['year']))?"class='actual'":"href='".$this->qs(array('year'=>'4', 'opt'=>1), array('page'=>1))."'"?>>80's</a></li>
            <li><a <?=(preg_match("/^5$/", $this->qs['year']))?"class='actual'":"href='".$this->qs(array('year'=>'5', 'opt'=>1), array('page'=>1))."'"?>>90's</a></li>
            <li><a <?=(preg_match("/^6$/", $this->qs['year']))?"class='actual'":"href='".$this->qs(array('year'=>'6', 'opt'=>1), array('page'=>1))."'"?>>00's</a></li>
            <li><a <?=(preg_match("/^7$/", $this->qs['year']))?"class='actual'":"href='".$this->qs(array('year'=>'7', 'opt'=>1), array('page'=>1))."'"?>><?=$this->translate('Last year');?></a></li>
        </ul>
        <?php endif; ?>
    </div>
    
<?php if ($this->info['total']>0): ?>
    <div id="results">
    <ul>
       <?php foreach ($this->paginator as $key => $val):
            if ($val['filename']):?>
        <li class="file_excerpt">
            <h3>
                <a href="/<?=$this->lang?>/download/<?=$val['dlink'] ?>"><?=$val['filename'] ?></a>
                <?php 
                if ($val['votes'][1]) echo "<span class='useract votes_up'>{$val['votes'][1]}</span>";
                if ($val['votes'][2]) echo "<span class='useract votes_down'>{$val['votes'][2]}</span>";
                if ($val['comments']) echo "<span class='useract comments'>{$val['comments']}</span>";
                if (APPLICATION_ENV!='production'):?>
                    <span class="debuginfo">id: <?=$key ?> w:<?=$val['weight'] ?> fw:<?=$val['fileweight'] ?> fnc:<?=$val['attrs']['fncount'] ?> s:<?=$val['isources'] ?></span>
                <?php endif;?>
            </h3>
            <span>
              <?php
                if ($val['type']!=null) {
                    $func = 'format'.$val['type'];
                    echo $func($this, $val['md'], !$val['in_filename']);
                }
              ?></span>
              <span>
                <?php if ($val['size']) echo $val["size"]." - ";?>
            <?php 
            if ($val["sources"]) {
                foreach ($val["sources"] as $k => $v) 
                {
                    if ($v['links']) {
                        $links = array_splice($v['links'],-3);
                    }else {
                        if ($v['count']>1) 
                            $info = "${v['tip']}&nbsp;({$v['count']}&nbsp;".$this->translate('Sources').")";
                        else
                            $info = "${v['tip']}";
                        $links = array($info=>$v['link']);
                    }
                    foreach ($links as $info=>$link) {
                     if ($v['count']) {
                        echo "<a class='source' title='${v['tip']}' href='$link'>" . '<img alt="'.$v['tip'].' " src=" ' . STATIC_PATH .'/images/icons/'.$k .'d.gif" style="vertical-align:middle"><span>'.$info.'</span></a>&nbsp;&nbsp;';
                     }
                    }
                }
            } ?>
            </span>
        </li>
        <?php elseif (APPLICATION_ENV!='production'):?>
            <span class="debuginfo" style="background: red">id: <?=$key ?> w:<?=$val['weight'] ?> fw:<?=$val['fileweight'] ?> fnc:<?=$val['attrs']['fncount'] ?> s:<?=$val['isources'] ?></span>
        <?php
              endif;
            endforeach; ?>
    </ul>
    </div>
    <?=$this->paginationControl($this->paginator,'Sliding','paginator_search.phtml'); ?>
<?php else: ?>
    
    <h2><?=$this->translate('No results for:').' ';?><?php foreach (explode(' ', $this->info['q']) as $word) { echo "&nbsp;<a class='actual' href='".$this->qs(array('q'=>$word), array('page'=>1))."'>$word</a>"; }?>
    </h2>
    
    <?php if ($this->info["notypecount"]): ?>
        <p id="no_results"><?=sprintf($this->translate("NoTypeCount"), $this->qs['type'], $this->info['notypecount'], $this->qs(array(), array('page'=>1, 'type'=>1)));?></p>
    <?php endif;?>
<?php endif;?>
</div>